package com.danicsoft.daide.command;

import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap.Commands;

/**
 * Implements the ADM command. From Eric Wald's <a
 * href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>,
 * v0.14:
 * <h4><a name="ADM">client_request = ADM</a> ('name') ('message')</h4>
 * <p>
 * This message is sent from the client to the server, and contains an admin
 * message &mdash; that is, a message to do with the running of the game (for
 * example, I'm having connection problems, or server going down, or I have to
 * leave shortly). It can be sent by any client &mdash; player or observer
 * &mdash; and can be sent before, during or after the game. It should not be
 * used for negotiation. The server may interpret the message as a command, or
 * it may forward the message to every client. The server may be configured to
 * refuse Admin messages, in which case it will reply to the client with <b>REJ
 * (ADM ('name') ('message'))</b>.
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class AdminMessageCommand extends Command {

	/**
	 * The name of the sender of this message.
	 */
	private String name;

	/**
	 * The message.
	 */
	private String message;

	/**
	 * Construct a new <code>AdminMessageCommand</code> from the tokens given.
	 * 
	 * @param tokens
	 *        The tokens to construct this Admin Message from. They should
	 *        consist of two TokenGroups, each having a string, the first a
	 *        name, the second, a message.
	 * @throws ParseException
	 *         If there was a problem reading from the tokens.
	 */
	public AdminMessageCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	/**
	 * Construct a new <code>AdminMessageCommand</code> from <code>name</code>
	 * containing <code>message</code>.
	 * 
	 * @param name
	 *        The name of the person this message is sent from.
	 * @param message
	 *        The message itself.
	 */
	public AdminMessageCommand(String name, String message) {
		this.name = name;
		this.message = message;
	}

	@Override
	public byte getType() {
		return Commands.ADM;
	}

	@Override
	public String toString() {
		return "From " + name + ":\n" + message;
	}

	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		name = tokenIO.doText(tokenIO.subGroup(tokens), name);
		message = tokenIO.doText(tokenIO.subGroup(tokens), message);

	}

	/**
	 * Gets the name of the person who sent the message.
	 * 
	 * @return The name of the person who sent the message.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the message that was sent.
	 * @return The message;
	 */
	public String getMessage() {
		return message;
	}

}
